﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesRaster;

namespace ISchool
{
    class IAddData
    {
        //作者：代兴梅
        //描述：针对不同数据进行添加
        //枚举添加图像类型
         public enum mDocmentType
        {
             NULL,
            SHP,
            GDB,
            RASTER,
            MDB
        }
        private AxMapControl m_MapContorl;

        //添加数据地址
        private string addDataUrl = "";

        //共有数据添加地址
        public string AddDataURL
        {
            get { return addDataUrl; }
            set { addDataUrl = value; }
        }

        //实例化图像添加到的控件
        public IAddData(AxMapControl testMap)
        {
            m_MapContorl = testMap;
        }
        //确定加载文件类型
        public mDocmentType getDataType(string addDataUrl)
        {
            string datatype = System.IO.Path.GetExtension(addDataUrl);
            if (datatype == ".shp")
            {
                return mDocmentType.SHP;
            }
            else if (datatype == ".gdb")
            {
                return mDocmentType.GDB;
            }
            else if (datatype == ".mdb")
            {
                return mDocmentType.MDB;
            }
            else if (datatype == ".tif" || datatype == ".jpg" || datatype == ".bmp" || datatype == ".img")
            {
                return mDocmentType.RASTER;
            }
            else
            {
                MessageBox.Show("暂时不支持该格式");
                return mDocmentType.NULL;
                
            }
        }
        //添加栅格图像
        public void addData_Raster(string Data_Raster)
        {
            addDataUrl = Data_Raster;
            if (addDataUrl != "")
            {
                if (getDataType(addDataUrl) != mDocmentType.RASTER) return;
                //创建栅格数据集
                IRasterLayer mRasterLayer = new RasterLayerClass();
                //从栅格数据集中创建栅格文件
                mRasterLayer.CreateFromFilePath(addDataUrl);
                //转换Layer并加载到MapConTrol中
                this.m_MapContorl.Map.AddLayer(mRasterLayer as ILayer);
                this.m_MapContorl.Extent = this.m_MapContorl.FullExtent;
                this.m_MapContorl.ActiveView.Refresh();
            }
            else
            {
                MessageBox.Show("数据未能正确加载");
            }
        }

        //添加Mdb数据
        public void addData_Mdb(string Data_Mdb)
        {
            
            addDataUrl = Data_Mdb;
            if (addDataUrl != "")
            {
                if (getDataType(addDataUrl) != mDocmentType.MDB) return;
                List<IDataset> pDatasets = new List<IDataset>();
                List<IFeatureClass> pFeatureClasses = new List<IFeatureClass>();
                //打开数据并置入pFeatureClasses链表中
                pFeatureClasses = this.openMdb(addDataUrl);

                //遍历
                foreach (IFeatureClass pFeatureClass in pFeatureClasses)
                {
                    IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass = pFeatureClass;
                    m_MapContorl.AddLayer((ILayer)pFeatureLayer);
                }

                //MapControl全屏设置并刷新
                this.m_MapContorl.Extent = this.m_MapContorl.FullExtent;
                this.m_MapContorl.ActiveView.Refresh();
            }
            else
            {
                MessageBox.Show("数据未能正确加载");
            }
        }

        //添加Gdb数据
        public void addData_Gdb(string Data_Gdb)
        {
            addDataUrl = Data_Gdb;
            if (addDataUrl != "")
            {
                if (getDataType(addDataUrl) != mDocmentType.GDB) return;
                //获取gdb文件夹目录
                string gdbFilePath = System.IO.Path.GetDirectoryName(Data_Gdb);
                //获取gdb的文件夹名称
                string gdbPath = System.IO.Path.GetFileName(Data_Gdb);
                //新建IFeatureClass链表（等待数据导入）
                List<IFeatureClass> pFeatureClasses = new List<IFeatureClass>();
                //设置数据处理空间
                IWorkspaceFactory testmY = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory();
                //打开GDB文件并作为当前工作空间
                IWorkspace pWorkspace = testmY.OpenFromFile(Data_Gdb, 0);
                //数据页集
                IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny);
                //数据页指针指向零处索引
                pEnumDataset.Reset();
                //数据页
                IDataset pDataset = pEnumDataset.Next();
                //获取表中名字
                List<string> IName = new List<string>();
                //遍历数据页，并导入IFeatureClass
                while (pDataset != null)
                {
                    if (pDataset is IFeatureClass)
                    {
                        string strDatasestName = pDataset.Name;
                        pFeatureClasses.Add(pDataset as IFeatureClass);
                        IName.Add(pDataset.Name);
                    }
                    pDataset = pEnumDataset.Next();
                }
                int il = 0;
                //遍历IFeatureClass，并逐一加载入MapControl中去
                foreach (IFeatureClass mFeature in pFeatureClasses)
                {
                    IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass = mFeature;
                    ILayer mLayer = pFeatureLayer as ILayer;
                    //对图层恢复名称
                    mLayer.Name = IName[il];
                    this.m_MapContorl.AddLayer(mLayer);
                    il++;
                }
            }
            else
            {
                MessageBox.Show("数据未能正确加载");
            }
        }

        //添加Shp数据
        public void addData_Shp(string Data_Shp)
        {
            addDataUrl=Data_Shp;
            if (addDataUrl != "")
            {
                if (getDataType(addDataUrl) != mDocmentType.SHP) return;
                string strFilePath = System.IO.Path.GetDirectoryName(Data_Shp);
                string streFileName = System.IO.Path.GetFileName(Data_Shp);
                this.m_MapContorl.AddShapeFile(strFilePath, streFileName);
                this.m_MapContorl.Extent = this.m_MapContorl.FullExtent;
            }
            else
            {
                MessageBox.Show("数据未能正确加载");
            }
        }

        //打开Mdb并将数据置入IFeatureClass链表中
        private List<IFeatureClass> openMdb(string fileName1)
        {
            //要素集链表
            List<IFeatureClass> pFeatureClasses = new List<IFeatureClass>();
            //工作空间
            IWorkspaceFactory pAccessFactory = new AccessWorkspaceFactoryClass();
            //工作区
            IWorkspace pWorkspace = pAccessFactory.OpenFromFile(fileName1, 0);
            //数据页集，数据读入任何esriDatasetType
            IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny);
            //数据指针指向第零条记录
            pEnumDataset.Reset();
            //数据指针指向第一条记录
            IDataset pDataset = pEnumDataset.Next();

            //遍历数据记录并载入pFeatureClasses
            while (pDataset != null)
            {
                //遍历要素集
                if (pDataset is IFeatureDataset)
                {
                    string strDatasestName = pDataset.Name;
                    //创建要素空间
                    IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
                    //打开要素集
                    IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(strDatasestName);
                    //导入数据表（各种格式）
                    IEnumDataset pEnumDataset2 = pFeatureDataset.Subsets;
                    //数据指向第零条记录
                    pEnumDataset2.Reset();
                    //数据指向第一条
                    IDataset pDataset2 = pEnumDataset2.Next();
                    //再次遍历要素集下的要素并数据导入
                    while (pDataset2 != null)
                    {
                        if (pDataset2 is IFeatureClass)
                        {
                            pFeatureClasses.Add(pDataset2 as IFeatureClass);
                        }
                        //指向下一条数据（循环）
                        pDataset2 = pEnumDataset2.Next();
                    }
                }
                //指向下一条数据集（循环）
                pDataset = pEnumDataset.Next();
            }
            return pFeatureClasses;
        }
    }
}
